//
//  MallNewspaperViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/18.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "MallNewspaperViewController.h"
#import "PeriodicalSearchView.h"
#import "BookCollectCell.h"
#import "FooterCollectionReusableView.h"
#import "NewsaperDetailViewController.h"
#import "XinxinTools.h"
#import <WebKit/WebKit.h>

@interface MallNewspaperViewController () <UICollectionViewDelegate,UICollectionViewDataSource,WKNavigationDelegate>

@property (nonatomic, strong)PeriodicalSearchView *searchView;
@property (nonatomic, strong)UICollectionView *listCollectionView;

@property (nonatomic, strong)WKWebView *webView;
@property (nonatomic, strong)UIButton * btn;

@end

@implementation MallNewspaperViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self buildUI];
}

- (void)buildUI {
    [self.view addSubview:self.webView];
    
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(5, 5, 80, 40);
    _btn.backgroundColor = MainGreenColor;
    [_btn setTintColor:[UIColor whiteColor]];
    _btn.layer.cornerRadius = 5;
    _btn.layer.masksToBounds = YES;
    _btn.hidden = YES;
    [_btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [_btn setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:_btn];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:NewsPaperURL]]];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    NSString*injectionJSString =@"var script = document.createElement('meta');"
    
    "script.name = 'viewport';"
    
    "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=3.0, minimum-scale=1.0, user-scalable=yes\";"
    
    "document.getElementsByTagName('head')[0].appendChild(script);";

    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
}

//
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    if (navigationAction.targetFrame == nil) {
        [webView loadRequest:navigationAction.request];
    }
    NSString *url1=navigationAction.request.URL.absoluteString;
    //2.做你想做的操作
    if (![url1 isEqualToString:NewsPaperURL]) {
        self.btn.hidden = NO;
    } else {
        self.btn.hidden = YES;
    }
    decisionHandler(YES);
}

//-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//    //1.截取到当前地址
//    NSString *url1=request.URL.absoluteString;
//    //2.做你想做的操作
//    if (![url1 isEqualToString:NewsPaperURL]) {
//        self.btn.hidden = NO;
//        return YES;
//    } else {
//        self.btn.hidden = YES;
//        return YES;
//    }
//    return YES;
//}

- (void)clickBack{
    if ([_webView canGoBack]) {
        [_webView goBack];
    }
}

#pragma mark - 懒加载
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight)];
        _webView.navigationDelegate = self;
        
    }
    return _webView;
}
- (PeriodicalSearchView *)searchView {
    if (!_searchView) {
        _searchView = [[PeriodicalSearchView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 60)];
    }
    return _searchView;
}

- (UICollectionView *)listCollectionView {
    if (!_listCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth ) / 3, 180);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.footerReferenceSize = CGSizeMake(kScreenWidth, 65);
        _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.searchView.bottom, kScreenWidth, 625) collectionViewLayout:layout];
        _listCollectionView.delegate = self;
        _listCollectionView.dataSource = self;
        _listCollectionView.backgroundColor = RGB(238, 238, 238);
        _listCollectionView.showsVerticalScrollIndicator = NO;
        _listCollectionView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
        [_listCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
        [_listCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([FooterCollectionReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([FooterCollectionReusableView class])];
    }
    return _listCollectionView;
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        FooterCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([FooterCollectionReusableView class]) forIndexPath:indexPath];
        return view;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NewsaperDetailViewController *detailVC = [[NewsaperDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
}




@end
